<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="关键词" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入评论关键词搜索"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ID、名称" prop="userParam">
        <el-input
          v-model="queryParams.userParam"
          placeholder="请输入用户ID、名称搜索"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论时间">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="handleDateChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-select v-model="queryParams.categoryId">
          <el-option
            v-for="item in classifyData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="mb8" style="display: flex;align-items: center;justify-content: space-between">
      <el-col class="tag-group">
        <el-tag
          v-for="(item, index) in tagItems"
          :key="item.label"
          :effect="tagIndex === index ? 'dark' : 'plain'"
          style="margin-right: 10px"
          @click="handleClick(index)">
          {{ item.label }}
        </el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="default"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </div>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" width="80" align="center" sortable />
      <el-table-column label="评论内容" align="center" prop="content" />
      <el-table-column label="评论用户ID" align="center" prop="userId" />
      <el-table-column label="评论用户名" align="center" prop="username" />
      <el-table-column label="评论时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属模板" align="center" prop="templateContent">
        <template slot-scope="scope" v-if="scope.row.templateType === 1 ? scope.row.templateCover : scope.row.templateContent">
          <el-image :src="scope.row.templateType === 1 ? scope.row.templateCover : scope.row.templateContent" @click="handleView(scope.row)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="所属标题" align="center" prop="templateTitle" />
      <el-table-column label="所属分类" align="center" prop="categoryName" />
      <el-table-column label="审核状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.checkStatus === 1 ? '待审核' : scope.row.checkStatus === 2 ? '已通过' : '未通过' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.checkStatus === 1 || scope.row.checkStatus === 3"
            @click="handleCheck(scope.row, 2)"
          >通过审核</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.checkStatus === 1"
            @click="handleCheck(scope.row, 3)"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #f00"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员管理对话框 -->
    <el-dialog title="内容查看" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-image :src="content.templateContent" v-if="content.templateType === 2"></el-image>
      <video :src="content.templateCover" v-else-if="content.templateType === 1"></video>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getCommentList, getCommentApprovalCount, checkCommentInfo, removeCommentInfo, getClassifylist } from "@/api/shuiyin/shuiyin";

  export default {
    name: "CommentManage",
    data() {
      return {
        // 上传地址
        uploadAction: process.env.VUE_APP_BASE_API + "/common/ossUpload",
        // 位置数据
        tagItems: [
          {
            value: 0,
            label: '全部'
          }, {
            value: 1,
            label: '已通过'
          }, {
            value: 2,
            label: '待审核'
          }, {
            value: 3,
            label: '未通过'
          }
        ],
        // 位置下标
        tagIndex: 0,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 租户管理表格数据
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          categoryId: '',
          checkStatus: null,
          commentBeginDate: null,
          commentEndDate: null,
          content: '',
          userParam: '',
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          appName: [
            { required: true, message: "软件名称不能为空", trigger: "blur" }
          ],
        },
        content: {},
        // 分类数据
        classifyData: []
      };
    },
    created() {
      this.getList();
      this.getClassify();
      this.getSetInfo();
    },
    methods: {
      /** 查询租户管理列表 */
      getList() {
        this.loading = true;
        getCommentList(this.queryParams).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 日期时间选择组件确定事件
      handleDateChange(event) {
        this.queryParams.commentBeginDate = event[0];
        this.queryParams.commentEndDate = event[1];
      },
      getSetInfo() {
        getCommentApprovalCount().then(response => {
          if (response.data) {
            const { data } = response;
            this.tagItems[1].label = `已通过(${data.checkSuccessNum})`;
            this.tagItems[2].label = `待审核(${data.preCheckNum})`;
            this.tagItems[3].label = `未通过(${data.checkFailNum})`;
          }
        })
      },
      getClassify() {
        getClassifylist({type: null, pageNum: 1,pageSize: 1000}).then(response => {
          this.classifyData = response.rows;
        })
      },
      // tag标签点击事件
      handleClick(index) {
        this.tagIndex = index;
        this.queryParams.checkStatus = index;
        this.queryParams.pageNum = 1;
        this.getList();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.commentBeginDate = null;
        this.queryParams.commentEndDate = null;
        this.dateRange = [];
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 审核按钮操作 */
      handleCheck(row, status) {
        // checkStatus	2.通过审核 3.驳回
        this.$modal.confirm(`是否确认${status === 2 ? '审核通过' : '驳回'}编号为${row.id}的数据项？`).then(function() {
          return checkCommentInfo({
            id: row.id,
            checkStatus: status
          });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess(`${status === 2 ? '审核通过' : '驳回'}成功`);
        }).catch(() => {});
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除所选数据项？').then(function() {
          return removeCommentInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 内容查看操作 */
      handleView(row) {
        this.content = JSON.parse(JSON.stringify(row));
        this.open = true;
      }
    }
  };
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 90px;
    line-height: 90px;
    text-align: center;
  }
  .avatar {
    width: 90px;
    height: 90px;
    display: block;
  }
</style>
